package com.example.net.oss;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * OSS下载默认是保存文件
 * Created by Long on 2019/7/24.
 */

public class FileStreamDownloadCallback implements StreamDownLoadCallback{
    private String path; //文件保存路径
    public FileStreamDownloadCallback(final String path){
        this.path = path;
    }
    @Override
    public void process(InputStream is) throws IOException {
        if(is != null) {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int readBytes = 0;
            while ((readBytes = is.read(buffer)) > 0) {
                fos.write(buffer, 0, readBytes);
            }
            fos.flush();
            fos.close();
        }
    }
}
